package com.itbuka.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itbuka.domain.User;

import com.itbuka.user.mapper.UserMapper;
import com.itbuka.user.service.UserService;
import com.itbuka.user.utils.JwtUtils;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ld
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2024-07-27 17:22:20
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getIsDelete, 0);
        return userMapper.selectList(lqw);
    }

    @Override
    public List<User> selectList(User user) {
        LambdaQueryWrapper<User> lqw = this.lqw(user);
        return userMapper.selectList(lqw);
    }

    @Override
    public Integer insert(User user) {
        Integer i = null;
        try {
            String hashpw = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
            user.setPassword(hashpw);
            i = userMapper.insert(user);
        } catch (Exception e) {
            return -2;
        }
        return i;
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                User user = new User();
                user.setId(Long.parseLong(id));
                user.setIsDelete(1);
                userMapper.updateById(user);
            }
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public Integer update(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public Page<User> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<User>) this.selectAll();
    }

    @Override
    public Page<User> pageList(User user, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<User>) this.selectList(user);
    }

    /**
     * 用户启用、停用
     */
    @Override
    public Integer status(Long id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        return userMapper.updateById(user);
    }

    @Override
    @Transactional
    public void resetPwd(List<Long> ids) {
        for (Long id : ids) {
            String hashpw = BCrypt.hashpw("123456", BCrypt.gensalt());
            User user = new User();
            user.setId(id);
            user.setPassword(hashpw);
            int i = userMapper.updateById(user);
            if (i != 1) {
                throw new RuntimeException("重置密码失败");
            }
        }
    }

    @Override
    public String login(String username, String password) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        lqw.eq(User::getIsDelete, 0);
        User user = userMapper.selectOne(lqw);
        if (user == null) {
            return "-2";
        }
        if (user.getStatus() == 1) {
            return "-3";
        }
        if (!BCrypt.checkpw(password, user.getPassword())) {
            return "-1";
        }

        Map map = new HashMap();
        map.put("id", user.getId());
        map.put("username", user.getUsername());
        map.put("role", user.getRole());
        String token = JwtUtils.encrypt(map, new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 7));
        return token;
    }


    public LambdaQueryWrapper<User> lqw(User user) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        if (user.getId() != null) {
            lqw.eq(User::getId, user.getId());
        }
        if (user.getUsername() != null) {
            lqw.eq(User::getUsername, user.getUsername());
        }
        if (user.getAvatar() != null) {
            lqw.eq(User::getAvatar, user.getAvatar());
        }
        if (user.getPassword() != null) {
            lqw.eq(User::getPassword, user.getPassword());
        }
        if (user.getPhoneNum() != null) {
            lqw.eq(User::getPhoneNum, user.getPhoneNum());
        }
        if (user.getEmail() != null) {
            lqw.eq(User::getEmail, user.getEmail());
        }
        if (user.getNickname() != null) {
            lqw.eq(User::getNickname, user.getNickname());
        }
        if (user.getDepartment() != null) {
            lqw.eq(User::getDepartment, user.getDepartment());
        }
        if (user.getRole() != null) {
            lqw.eq(User::getRole, user.getRole());
        }
        if (user.getStatus() != null) {
            lqw.eq(User::getStatus, user.getStatus());
        }
        if (user.getIsDelete() != null) {
            lqw.eq(User::getIsDelete, user.getIsDelete());
        }
        if (user.getCreateTime() != null) {
            lqw.eq(User::getCreateTime, user.getCreateTime());
        }
        if (user.getUpdateTime() != null) {
            lqw.eq(User::getUpdateTime, user.getUpdateTime());
        }
        return lqw;
    }
}




